home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / clock.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  166 lines

  1. /*
  2.  * clock.c
  3.  *
  4.  * Forms Object class: CLOCK
  5.  *
  6.  * Written by: Mark Overmars (based on clock by Paul Haeberli)
  7.  *
  8.  * Version 2.1 a
  9.  * Date: Sep 29, 1992
  10.  */
  11.  
  12. #include "forms.h"
  13.  
  14. #include <malloc.h>
  15. #include <sys/types.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include "gl/gl.h"
  19. #include "gl/device.h"
  20.  
  21. float hourhand[4][2] = { {-0.5,0.0},{0.0,-1.5},{0.5,0.0},{0.0,7.0}};
  22. float  minhand[4][2] = { {-0.5,0.0},{0.0,-1.5},{0.5,0.0},{0.0,11.5}};
  23. float  sechand[4][2] = { {-0.05,0.0},{0.0,-2.0},{0.05,0.0},{0.0,11.5}};
  24.  
  25. int hours,minutes,seconds;
  26.  
  27. static void showhands(int fillcolor,int bordercolor)
  28. {
  29.     pushmatrix();
  30.         rotate(-(hours*3600/12)-(minutes*5),'z');
  31.         fl_color(fillcolor); 
  32.     polf2(4,hourhand);
  33.         fl_color(bordercolor);
  34.     poly2(4,hourhand);
  35.     popmatrix();
  36.     pushmatrix();
  37.         rotate(-(minutes*3600/60)-(seconds),'z');    
  38.         fl_color(fillcolor); 
  39.     polf2(4,minhand);
  40.         fl_color(bordercolor);
  41.     poly2(4,minhand);
  42.     popmatrix();
  43.     pushmatrix();
  44.         rotate(-seconds*3600/60,'z');
  45.         fl_color(fillcolor); 
  46.     polf2(4,sechand);
  47.         fl_color(bordercolor);
  48.     poly2(4,sechand);
  49.     popmatrix();
  50.     pnt(0.0,0.0,0.0);
  51. }
  52.  
  53. static void draw_clock(int type,int col1,int col2)
  54. {
  55.   int i;
  56.   pushmatrix();
  57.     if (type == FL_ROUND_CLOCK)
  58.     {
  59.       fl_color(col1);  circf(0.0,0.0,14.0);
  60.       fl_color(BLACK); circ(0.0,0.0,14.0);
  61.     }
  62.     pushmatrix();
  63.       fl_color(52);
  64.       for (i=0; i<12; i++)
  65.       {
  66.         if (i==0) rectf(-0.5,9.0,0.5,11.0);
  67.         else if (i==3 || i==6 || i== 9) rectf(-0.5,9.5,0.5,10.5);
  68.         else rectf(-0.25,9.5,0.25,10.5);
  69.         rotate(-300,'z');
  70.       }
  71.     popmatrix();
  72.     pushmatrix();
  73.       translate(0.60,-0.60,0.0);
  74.       showhands(34,34);
  75.     popmatrix();
  76.     showhands(col2,54);
  77.   popmatrix();
  78.  
  79. /*--------------------------------------*/
  80.  
  81. /* Object specific information. */
  82.  
  83. typedef struct {
  84.   int sec;    /* Last number of seconds */
  85. } SPEC;
  86.  
  87. static int handle_clock(FL_OBJECT *ob,int event,float mx,float my,char key)
  88. {
  89.   long clock;
  90.   int c1;
  91.   struct tm *timeofday;
  92.   switch (event)
  93.   {
  94.     case FL_DRAW:
  95.     if (ob->type == FL_ROUND_CLOCK) c1 =FL_CLOCK_TOPCOL; else c1 = ob->col1;
  96.       fl_drw_box(ob->boxtype,ob->x,ob->y,ob->w,ob->h,c1,FL_BOX_BW);
  97.       fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,
  98.                 ob->lcol,ob->lsize,ob->lstyle,ob->label);
  99.         pushmatrix();
  100.         translate(ob->x+ob->w/2.0 - 0.5,ob->y+ob->h/2.0 - 0.5,0.0);
  101.         scale(ob->w/28.0,ob->h/28.0,1.0);
  102.     if (ob->type == FL_ROUND_CLOCK && ob->boxtype != FL_NO_BOX)
  103.       scale(0.85,0.85,1.0);
  104.     draw_clock(ob->type,ob->col1,ob->col2);
  105.     popmatrix();
  106.     return 0;
  107.     case FL_STEP:
  108.         time(&clock);
  109.         timeofday = (struct tm *)localtime(&clock);
  110.         hours = timeofday->tm_hour;
  111.         minutes = timeofday->tm_min;
  112.         seconds = timeofday->tm_sec;
  113.     if (((SPEC *)(ob->spec))->sec != seconds)
  114.     {
  115.       ((SPEC *)(ob->spec))->sec = seconds;
  116.       fl_redraw_object(ob);
  117.     }
  118.     return 0;
  119.     case FL_FREEMEM:
  120.     free(ob->spec);
  121.     return 0;
  122.   }
  123.   return 0;
  124. }
  125.  
  126. /*--------------------------------------*/
  127.  
  128. FL_OBJECT *fl_create_clock(int type,float x,float y,float w,float h,char label[])
  129. /* creates an object */
  130. {
  131.   FL_OBJECT *ob;
  132.   ob = fl_make_object(FL_CLOCK,type,x,y,w,h,label,handle_clock);
  133.   if (type == FL_ROUND_CLOCK)
  134.     ob->boxtype = FL_NO_BOX;
  135.   else
  136.     ob->boxtype = FL_CLOCK_BOXTYPE;
  137.   ob->col1 = FL_CLOCK_COL1;
  138.   ob->col2 = FL_CLOCK_COL2;
  139.   ob->lcol = FL_CLOCK_LCOL;
  140.   ob->align = FL_CLOCK_ALIGN;
  141.  
  142.   ob->spec = (int *)(fl_malloc(sizeof(SPEC)));
  143.  
  144.   ob->active = 1;
  145.   ob->automatic = 1;
  146.   return ob;
  147. }
  148.  
  149. FL_OBJECT *fl_add_clock(int type, float x, float y, float w, float h, char label[])
  150. /* Adds an object */
  151. {
  152.   FL_OBJECT *ob;
  153.   ob = fl_create_clock(type,x,y,w,h,label);
  154.   fl_add_object(fl_current_form,ob);
  155.   return ob;
  156. }
  157.  
  158. void fl_get_clock(FL_OBJECT *ob,int *h,int *m,int *s)
  159. /* returns the time */
  160. {
  161.   *h = hours;
  162.   *m = minutes;
  163.   *s = seconds;
  164. }
  165.